const Mysql=require('mysql-pro');
const conf=require('../config');

let db=new Mysql({
  mysql: conf.db
});

db.execute=async sql=>{
  await db.startTransaction();

  let res;

  if(typeof sql=='string'){
    res=await db.executeTransaction(sql);
  }else{
    sql.forEach(async item=>{
      res=await db.executeTransaction(item);
    });
  }

  await db.stopTransaction();

  return res;
};

module.exports= async (ctx,next)=>{
    ctx.db = db;
    await next();
};
